package com.example.myapplication;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OtherCloud  extends Activity {
    private TextView mTextview;

    private String getHashCode;
    String NoteId;
    private String CloudNoteContent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去除黑框
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_cloud);
        getHashCode=getIntent().getStringExtra("HashCode");
        getHashCode=getHashCode.substring(0,getHashCode.length()-3);
        System.out.println(getHashCode);
        int Value=Integer.valueOf(getHashCode);
        Value=((Value-3)/2);
        System.out.println(Value);
        NoteId=Integer.toString(Value);
        getContent();
        System.out.println(CloudNoteContent+"..................");

//        mTextview = (TextView)this.findViewById(R.id.Cloud_text);
//        hashCode=(TextView)findViewById(R.id.cloudhashCode);
//        NoteCloudId=getIntent().getIntExtra("NoteID",0);
//        System.out.println(NoteCloudId+"...............................");
//        mTextview.setText(getIntent().getStringExtra("Content"));
    }
    public void getContent( ) {//根据id找内容
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String Url="jdbc:mysql://cdb-l1c6g0ne.bj.tencentcdb.com:10166/AndroidUser?user=root&password=tzyTZY123";
                    String Driver="com.mysql.jdbc.Driver";
                    Connection connection = null;
                    PreparedStatement preparedStatement = null;
                    ResultSet resultSet;
                    Class.forName(Driver);
                    connection = DriverManager.getConnection(Url);
                    String MySql = "select * from Note where NoteNo=?";//?代表参数会在以后加
//            String MySql = "select * from android_user";
                    preparedStatement = connection.prepareStatement(MySql);
                    preparedStatement.setString(1, NoteId);//代表第一个？放置这个参数，该用户的所有笔记本
                    System.out.println(NoteId);
                    resultSet =  preparedStatement.executeQuery();
                    //遍历查询结果集
                    List<String> NoteContent=new ArrayList<>();
                    while(resultSet.next()){
                        NoteContent.add(resultSet.getString("Content"));
                    }
                    System.out.println(NoteContent);
                    CloudNoteContent=NoteContent.get(0);
                    mTextview=(TextView)findViewById(R.id.Other_Cloud_Note);
                    mTextview.setText(CloudNoteContent);
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                //2.创建连接
            }
        }).start();
    }
    //    public static String getHashCode(String code) {
//        int Value = Integer.valueOf(code);
//        Value =2*Value+3;
//        String Result=Integer.toString(Value)+"512";
//        return Result;
//    }
//    public static String decodeHashCode(String code){
//        code=code.substring(0,code.length()-3);
////        System.out.println(code);
//        int Value=Integer.valueOf(code);
//        return Integer.toString((Value-3)/2);
//    }
    public void goBackClick(View v) {
        finish();
    }
}
